package net.xdclass.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.enums.PluginTypeEnum;
import net.xdclass.manager.TrafficManager;
import net.xdclass.mapper.TrafficMapper;
import net.xdclass.model.TrafficDO;
import net.xdclass.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author: wangzhan
 * @Description: TODO
 * @DateTime: 2024/12/9 21:43
 **/
@Component
@Slf4j
public class TrafficManagerImpl implements TrafficManager {
    @Autowired
    private TrafficMapper trafficMapper;
    @Override
    public int add(TrafficDO trafficDO) {
       return trafficMapper.insert(trafficDO);
    }

    @Override
    public IPage<TrafficDO> pageAvailable(int page, int size, long accountNo) {
        IPage<TrafficDO> iPage = new Page(page,size);
        String today = TimeUtil.format(new Date(), "yyyy-MM-dd");
        IPage<TrafficDO> trafficDOIPage = trafficMapper.selectPage(iPage, new LambdaQueryWrapper<TrafficDO>().eq(TrafficDO::getAccountNo, accountNo)
                .ge(TrafficDO::getExpiredDate,today).orderByDesc(TrafficDO::getGmtCreate)
        );
        return trafficDOIPage;
    }

    @Override
    public int addDayUsedTimes(Long currentTrafficId, long accountNo, int dayUsedTimes) {
        return trafficMapper.addDayUsedTimes(currentTrafficId,accountNo,dayUsedTimes);
    }

    @Override
    public TrafficDO findByIdAndAccountNo(Long trafficId, long accountNo) {
        LambdaQueryWrapper<TrafficDO> wrapper = new LambdaQueryWrapper<TrafficDO>().eq(TrafficDO::getId, trafficId)
                .eq(TrafficDO::getAccountNo, accountNo);
        TrafficDO trafficDO = trafficMapper.selectOne(wrapper);
        return trafficDO;
    }

    @Override
    public boolean deleteExpiredTraffic() {
        int rows = trafficMapper.delete(new LambdaQueryWrapper<TrafficDO>(
        ).le(TrafficDO::getExpiredDate, new Date()));
        log.info("删除过期流量包影响行数:{}",rows);
        return true;
    }

    /**
     * 查找未过期流量包列表(不一定可用，可能超过次数)和免费流量包
     * /SELECT * FROM traffic_0 WHERE (account_no= ? AND (expired_date >= ? OR out_trade_no = ?))
     * @param accountNo
     * @return
     */
    @Override
    public List<TrafficDO> selectAvailableTraffics(Long accountNo) {
        String today = TimeUtil.format(new Date(),"yyyy-MM-dd");
        LambdaQueryWrapper<TrafficDO> queryWrapper = new LambdaQueryWrapper<TrafficDO>().eq(TrafficDO::getAccountNo, accountNo)
                .eq(TrafficDO::getPluginType, PluginTypeEnum.SHORT_LINK)
                .and(wrapper -> wrapper.ge(TrafficDO::getExpiredDate, today)
                        .or().eq(TrafficDO::getOutTradeNo,"free-init"));
        List<TrafficDO> trafficDOS = trafficMapper.selectList(queryWrapper);
        return trafficDOS;
    }

    @Override
    public int batchUpdateUsedTimes(Long accountNo, List<Long> unUpdatedTrafficIds) {
        int rows = trafficMapper.update(null, new LambdaUpdateWrapper<TrafficDO>().eq(TrafficDO::getAccountNo, accountNo)
                .in(TrafficDO::getId, unUpdatedTrafficIds)
                .set(TrafficDO::getDayUsed, 0)
        );
        return rows;
    }

    @Override
    public int releaseUsedTimes(long accountNo, Long trafficId, Integer useTimes, String useDate) {
        return trafficMapper.releaseUsedTimes(trafficId,accountNo,useTimes,useDate);
    }
}
